home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / PREDEF4.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  24KB  |  895 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /*    +---------------------------------------------------+
  10.       |                                                   |
  11.       |          I N T E R P     P R E D E F S            |
  12.       |         Part 4: Input/Output Procedures           |
  13.       |                  (C Version)                      |
  14.       |                                                   |
  15.       |   Adapted From Low Level SETL version written by  |
  16.       |                                                   |
  17.       |                  Monte Zweben                     |
  18.       |               Philippe Kruchten                   |
  19.       |               Jean-Pierre Rosen                   |
  20.       |                                                   |
  21.       |    Original High Level SETL version written by    |
  22.       |                                                   |
  23.       |                   Clint Goss                      |
  24.       |               Tracey M. Siesser                   |
  25.       |               Bernard D. Banner                   |
  26.       |               Stephen C. Bryant                   |
  27.       |                  Gerry Fisher                     |
  28.       |                                                   |
  29.       |              C version written by                 |
  30.       |                                                   |
  31.       |               Robert B. K. Dewar                  |
  32.       |                                                   |
  33.       +---------------------------------------------------+
  34. */
  35.  
  36. /*  This module contains routines for the implementation of some of
  37.  *  the predefined Ada packages and routines, namely SEQUENTIAL_IO,
  38.  *  DIRECT_IO, TEXT_IO, and CALENDAR. Part 4 contains the initialization
  39.  *  and termination routines for predef, and the basic I/O routines
  40. */
  41.  
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #ifdef IBM_PC
  45. #include <io.h>
  46. #endif
  47. #include "ipredef.h"
  48. #include "miscp.h"
  49. #include "predefp.h"
  50.  
  51. static void check_ifile_closed(int *);
  52. static void check_xfile_closed(char *);
  53. static void open_file();
  54. #ifdef IBM_PC
  55. #undef putc
  56. #define putc(A, B) fputc((A),(B));fflush(B)
  57. #endif
  58. #ifdef GWUMON
  59. #undef putc
  60. #define putc(A, B) CWK_PUTC((A),(B))
  61. #endif
  62. #ifdef DEBUG_PREDEF
  63. static void gchar(char *, int);
  64. static void pchar(char *, int);
  65. #endif
  66.  
  67. /* AFCB for STANDARD_IN_FILE */
  68.  
  69. static struct afcb  in_afcb = {
  70.  
  71.     0,                          /* file descriptor for standard input */
  72.     "",                         /* file name(null) */
  73.     "",                         /* form string(null) */
  74.     TIO_IN_FILE,                /* mode, TEXT_IO input */
  75.     0, 0, 0,                    /* unused DIRECT_IO fields */
  76.     1,                          /* page number */
  77.     1,                          /* line number */
  78.     1,                          /* column number */
  79.     0,                          /* unbounded line length */
  80.     0,                          /* unbounded page length */
  81.     0, "  "                    /* look ahead */
  82. };
  83.  
  84. #ifdef IBM_PC
  85. /* keep track of last character read from stdin so can detect whether we
  86.  * need to flush what is left there before exiting
  87.  */
  88. static int last_char_input = EOF;
  89. #endif
  90.  
  91. /* AFCB for STANDARD_OUT_FILE */
  92.  
  93. static struct afcb  out_afcb = {
  94.  
  95.     0,                          /* file descriptor for standard input */
  96.     "",                         /* file name(null) */
  97.     "",                         /* form string(null) */
  98.     TIO_OUT_FILE,               /* mode, TEXT_IO output */
  99.     0, 0, 0,                    /* unused DIRECT_IO fields */
  100.     1,                          /* page number */
  101.     1,                          /* line number */
  102.     1,                          /* column number */
  103.     0,                          /* unbounded line length */
  104.     0,                          /* unbounded page length */
  105.     0, "  "                    /* look ahead */
  106. };
  107.  
  108. /* Procedure to initialize input/output data structures */
  109.  
  110. void initialize_predef()                     /*;initialize_predef*/
  111. {
  112.     /* Clear temporary file list, and clear AFCB vector */
  113.  
  114.     tfiles = 0;
  115.     for (filenum = 1; filenum <= MAXFILES; filenum++)
  116.         afcbs[filenum - 1] = 0;
  117.  
  118.     /* Setup references for current and standard files */
  119.  
  120.     current_in_file = 1;
  121.     current_in_file_saved = 1;
  122.     standard_in_file = 1;
  123.     afcbs[0] = &in_afcb;
  124.     filenum = 1;
  125.     IOFDESC = stdin;
  126.     CHARS = 0;
  127.  
  128.     current_out_file = 2;
  129.     current_out_file_saved = 2;
  130.     standard_out_file = 2;
  131.     afcbs[1] = &out_afcb;
  132.     filenum = 2;
  133.     IOFDESC = stdout;
  134.  
  135.     /* Set standard exception signalled on bad data (changed temporarily
  136.      * to CONSTRAINT_ERROR when TEXT_IO routines are called directly from
  137.      * the main interpretor for the IMAGE attribute.
  138.      */
  139.  
  140.     data_exception = DATA_ERROR;
  141. }
  142.  
  143.  
  144. /* CHECK_OPENED_OK */
  145.  
  146. /* Checks that an fopen succeeded, raise USE_ERROR if not */
  147.  
  148. void check_opened_ok()                                   /*;check_opened_ok*/
  149. {
  150.     if (IOFDESC == NULL)
  151.         predef_raise(USE_ERROR, "Error opening or resetting file");
  152. }
  153.  
  154. /* CHECK_IFILE_CLOSED */
  155.  
  156. /* Checks that the file object stored at file_ptr is closed */
  157.  
  158. static void check_ifile_closed(int *file_ptr)           /*;check_ifile_closed*/
  159. {
  160.     int     file_val;
  161.  
  162.     file_val = *file_ptr;
  163.     if (file_val != 0)
  164.         predef_raise(STATUS_ERROR, "File not closed");
  165. }
  166.  
  167. /* CHECK_XFILE_CLOSED */
  168.  
  169. /* Checks that no external file with a matching name is currently open */
  170.  
  171. static void check_xfile_closed(char *fname)             /*;check_xfile_closed*/
  172. {
  173.     int     i;
  174.     for (i = 1; i <= MAXFILES; i++) {
  175.         if (afcbs[i - 1] == NULL) continue;
  176.         if (strcmp(fname, afcbs[i - 1] -> io_fname) == 0 &&
  177.           afcbs[i - 1] -> io_fdesc != NULL) {
  178.             predef_raise(USE_ERROR, "File already open");
  179.         }
  180.     }
  181. }
  182.  
  183. /* CHECK_FILE_OPEN */
  184.  
  185. /* Check if the current file is open or not. If the file is not open,
  186.   * then STATUS_ERROR is raised. Otherwise control returns normally.
  187. */
  188.  
  189. void check_file_open()                                       /*;check_file_open*/
  190. {
  191.     if (filenum == 0)
  192.         predef_raise(STATUS_ERROR, "File not open");
  193. }
  194.  
  195. /* If the current file is not open, then STATUS_ERROR is raised. If
  196.  * the file is open, then the mode is checked against the argument which
  197.  * is the desired mode for the operation. If it does not match, then
  198.  * MODE_ERROR is raised, otherwise control returns normally.
  199.  */
  200.  
  201. void check_status(int c_mode)                                /*;check_status*/
  202. {
  203.     check_file_open();
  204.     if (IOMODE != c_mode)
  205.         predef_raise(MODE_ERROR, "Incorrect file status");
  206. }
  207.  
  208. /*  Routine called by the OPEN and CREATE portions of the PREDEF procedure
  209.  *  to perform common data structure operations for TEXT_IO operations.
  210.  *  The operation in opn is 'C' for a create and 'O' for an open.
  211.  */
  212.  
  213. void open_textio(char opn)                       /*;open_textio*/
  214. {
  215.     open_file();
  216.  
  217. #ifdef SYSTEM_V
  218.     if (strlen(IOFNAME) > 14) predef_raise(NAME_ERROR,"Invalid file name");
  219. #endif
  220.  
  221.     if (opn == 'C') {
  222.         IOFDESC = fopen_txt(IOFNAME, "w");
  223.         if (IOFDESC == NULL) predef_raise(NAME_ERROR,"Invalid file name");
  224.         if (IOMODE == SIO_IN_FILE) {
  225.             fclose(IOFDESC);
  226.             IOFDESC = fopen_txt(IOFNAME, "r");
  227.             check_opened_ok();
  228.         }
  229.     }
  230.     else {                      /* opn == 'O' */
  231.         /*
  232.         * According to AI-00048: 
  233.         * Opening a file with IN_FILE mode which is the default output file 
  234.         * will raise MODE_ERROR. 
  235.         * Opening a file with OUT_FILE mode which is the default input file 
  236.         * will raise MODE_ERROR.
  237.         * The values to be checked is in current_in_file_saved and
  238.         * current_out_file_saved which are copies of the file numbers
  239.         * associated with the default files. These copies must be used
  240.         * because when the default files are closed their filenums saved
  241.         * in current_XXX_file are set to zero and therefore lost for this
  242.         * check.
  243.         */
  244.         if (filenum == current_in_file_saved && IOMODE == TIO_OUT_FILE)
  245.             predef_raise(MODE_ERROR,"File is default in file");
  246.         i